﻿var mainself = this;
mainself.hostWebUrl = "";
mainself.appWebContext = null;
mainself.hostWebContext = null;
mainself.hostWeb = null;
mainself.itemCollection = null;
mainself.listPreview = null;
mainself.currentUserPreview = null;

$(document).ready(function () {
    mainself.hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    mainself.appWebContext = new SP.ClientContext.get_current();
    mainself.hostWebContext = new SP.AppContextSite(mainself.appWebContext, mainself.hostWebUrl);
    mainself.hostWeb = mainself.hostWebContext.get_web();
    mainself.listPreview = mainself.hostWeb.get_webs();
    mainself.currentUserPreview = mainself.hostWeb.get_currentUser();
    appWebContext.load(mainself.listPreview);
    appWebContext.load(mainself.currentUserPreview);
    appWebContext.executeQueryAsync(GetNewInfo, onError);
});

function GetNewInfo() {
    var itemEnumerator = mainself.listPreview.getEnumerator();
    while (itemEnumerator.moveNext()) {
        if (itemEnumerator.get_current().get_title() == "SharePoint Object Explorer") {
            mainself.listPreview = itemEnumerator.get_current().get_lists().getByTitle("UsageStatistic");
            appWebContext.load(mainself.listPreview);
            appWebContext.executeQueryAsync(CheckList, onError);
        }

    }

}




function onQuerySucceeded(sender, args) {
    alert(currentUser.get_loginName());
}

function CheckList() {
    if (mainself.listPreview.get_itemCount() == 10)
        var r = confirm("You already use our application for a while now, would you like to write a review for us, tell us what you think about our program?");
    if (r == true) {
        window.location.href = 'http://office.microsoft.com/en-us/store/sharepoint-object-explorer-WA104055379.aspx?queryid=1254d8bb-2652-42ad-8fcb-284e543520b4&css=sharepoint%20object%20explorer';

    }
    else {
    }
}

function onError(err) {
    mainself.d = err;
}
